package com.yfve.eol.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yfve.eol.Constants;
import com.yfve.eol.EolApplication;
import com.yfve.eol.util.Rl78Util;
import com.yfve.eol.util.Util;

import java.util.Arrays;

public class AgingTestService extends Service {
    private static final String TAG = "AgingTestService";

    private static final String KEY_START_AGING_TEST = "key_start_ageing_test";

    private static final int MSG_ID_CLOSE_BACK_LIGHT = 0x001;
    private static final int MSG_ID_OPEN_BACK_LIGHT = 0x002;
    private static final int AGING_TEST_TOTAL_CNT = 6;
    private static final int OPEN_BACK_LIGHT_DURATION = 9 * 60 * 1000;
    private static final int CLOSE_BACK_LIGHT_DURATION = 60 * 1000;
    private int mAgingTestCnt = 0;
    @SuppressLint("HandlerLeak")
    private Handler mAgingTestHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.i(TAG, "handleMessage: mAgingTestCnt = " + mAgingTestCnt);
            switch (msg.what) {
                case MSG_ID_OPEN_BACK_LIGHT:
                    mAgingTestCnt++;
                    Rl78Util.getInstance().setRl78PowerEnable(true);
                    if (mAgingTestCnt < AGING_TEST_TOTAL_CNT) {
                        mAgingTestHandler.sendEmptyMessageDelayed(MSG_ID_CLOSE_BACK_LIGHT, OPEN_BACK_LIGHT_DURATION);
                    } else {
                        mAgingTestHandler.removeCallbacksAndMessages(null);
                    }
                    break;
                case MSG_ID_CLOSE_BACK_LIGHT:
                    mAgingTestCnt++;
                    Rl78Util.getInstance().setRl78PowerEnable(false);
                    mAgingTestHandler.sendEmptyMessageDelayed(MSG_ID_OPEN_BACK_LIGHT, CLOSE_BACK_LIGHT_DURATION);
                    break;
                default:
                    break;
            }
        }
    };


    public static void handleSetAgingTestCmd(int id, byte[] values) {
        Log.i(TAG, "handleSetAgingTestCmd: id = " + id + ", values = " + Arrays.toString(values));
        if (values.length < 1) {
            Log.e(TAG, "handleSetAgingTestCmd: wrong length");
            Util.setByteProperty(id, false);
            return;
        }

        Intent intent = new Intent(EolApplication.getAppContext(), AgingTestService.class);

        if (values[0] == Constants.VAL_0X01) {
            intent.putExtra(KEY_START_AGING_TEST, true);
        } else {
            intent.putExtra(KEY_START_AGING_TEST, false);
        }

        EolApplication.getAppContext().startService(intent);

        Util.setByteProperty(id, true);
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {

        if (intent != null) {
            if (intent.getBooleanExtra(KEY_START_AGING_TEST, false)) {
                mAgingTestCnt = 0;
                mAgingTestHandler.sendEmptyMessage(MSG_ID_OPEN_BACK_LIGHT);
            } else {
                mAgingTestHandler.removeCallbacksAndMessages(null);
                mAgingTestCnt = 0;
                Rl78Util.getInstance().setRl78PowerEnable(true);
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
